package data

import (
	"errors"
	"github.com/360EntSecGroup-Skylar/excelize"
	"github.com/gin-gonic/gin"
	"path"
	"server/model"
	"strconv"
)

func getSlideItems(rows [][]string, startIdx int) []model.SlideItem {
	var result []model.SlideItem
	for i := startIdx; i < len(rows); i++ {
		if i != startIdx && rows[i][2] != "" {
			break
		}
		spec := rows[i][16]
		if len(rows[i]) < 18 || (rows[i][17] != "y" && rows[i][17] != "Y") {
			spec = spec + "|N"
		} else {
			spec = spec + "|Y"
		}
		if len(rows[i]) < 19 || (rows[i][18] != "y" && rows[i][18] != "Y") {
			spec = spec + "|N"
		} else {
			spec = spec + "|Y"
		}
		if len(rows[i]) < 20 || (rows[i][19] != "y" && rows[i][19] != "Y") {
			spec = spec + "|N"
		} else {
			spec = spec + "|Y"
		}
		if len(rows[i]) < 21 || (rows[i][20] != "y" && rows[i][20] != "Y") {
			spec = spec + "|N"
		} else {
			spec = spec + "|Y"
		}
		result = append(result, model.SlideItem{
			Spec: spec,
		})
	}
	return result
}

func GetNarrationItems(male bool, target string, instruction string, tour int, lesson int, exercise int) (*gin.H, error) {
	var names = make(map[int]string)
	names[4] = "Q=ti,A=v"
	names[5] = "Q=tv,A=i"
	names[6] = "Q=iv,A=t"
	names[7] = "Q=v,A=ti"
	names[8] = "Q=i,A=tv"
	names[9] = "Q=t,A=iv"
	names[10] = "Q=t,A=i"
	names[11] = "Q=t,A=v"
	names[12] = "Q=v,A=t"
	names[13] = "Q=v,A=i"
	names[14] = "Q=i,A=t"
	names[15] = "Q=i,A=v"
	file, err := excelize.OpenFile("/home/sftp_root/shared/script_index.xlsx")
	if err != nil {
		return nil, err
	}
	rows, err := file.GetRows("Sheet1")
	var folder string
	var fileName string
	for index, row := range rows {
		if index == 0 {
			continue
		}
		if ((row[3] == "m" && male) || (row[3] == "f" && !male)) && (row[0] == target) && (row[1] == instruction) {
			folder = row[2]
			fileName = row[5]
			break
		}
	}
	if folder == "" || fileName == "" {
		return nil, errors.New("invalid folder or file")
	}
	scriptPath := path.Join("/home/sftp_root/shared", folder, fileName)
	file, err = excelize.OpenFile(scriptPath)
	if err != nil {
		return nil, err
	}
	currentTour := 0
	currentLesson := 0
	currentActivity := ""
	currentExercise := 0

	narrationFileName := ""

	rows, err = file.GetRows("Sheet1")
	if err != nil {
		return nil, err
	}
	for index, row := range rows {
		if index == 0 {
			continue
		}
		if row[0] != "" {
			currentTour, err = strconv.Atoi(row[0])
			if err != nil {
				return nil, err
			}
		}
		if tour == currentTour {
			if row[1] != "" {
				currentLesson, err = strconv.Atoi(row[1])
				if err != nil {
					return nil, err
				}
			}
			if lesson == currentLesson {
				if row[3] != "" {
					currentActivity = row[3]
				}
				if currentActivity == "Narration" {
					currentExercise, err = strconv.Atoi(row[4])
					if err != nil {
						return nil, err
					}
					if exercise == currentExercise {
						narrationFileName = row[5]
						break
					}
				}
			}
		}
	}

	if narrationFileName == "" {
		return nil, errors.New("exercise not found")
	}

	file, err = excelize.OpenFile(path.Join("/home/sftp_root/shared", folder, narrationFileName))
	if err != nil {
		return nil, err
	}

	rows, err = file.GetRows("Sheet1")
	if err != nil {
		return nil, err
	}

	currentSet := 0
	random := false
	var slides []model.NarrationItem

	for index, row := range rows {
		if index == 0 {
			continue
		}
		if len(row) < 17 {
			return nil, errors.New("invalid file on len(row) < 17. len(row) = " + strconv.Itoa(len(row)))
		}
		if row[0] != "" {
			currentSet, err = strconv.Atoi(row[0])
			if err != nil {
				return nil, errors.New("invalid file on strconv.Atoi(row[0])")
			}
			random = row[1] == "y" || row[1] == "Y"
		}
		if currentSet == exercise {
			if row[2] == "" {
				continue
			}
			currentSlide := row[2]
			currentSlide = currentSlide + "|"
			if row[3] == "y" || row[3] == "Y" {
				currentSlide = currentSlide + "Y|"
			} else {
				currentSlide = currentSlide + "N|"
			}
			for i := 4; i <= 16; i++ {
				if i == 16 {
					return nil, errors.New("invalid file on i == 16")
				}
				if row[i] == "Y" || row[i] == "y" {
					currentSlide = currentSlide + names[i]
					break
				}
			}
			slides = append(slides, model.NarrationItem{
				Spec:  currentSlide,
				Slide: getSlideItems(rows, index),
			})
		}
	}
	return &gin.H{
		"random": random,
		"slides": slides,
	}, nil
}
